import mongoose from "mongoose";
import conf from "../config/index";

const { host, port, dbName, user, password } = conf.mongodbConf;

let url = `mongodb://${host}:${port}`;
if (user && password) {
  url = `mongodb://${user}:${password}@${host}:${port}`;
}

mongoose.connect(`${url}/${dbName}?authSource=admin`, {
  user: "emo",
  pass: "Chen_emo_00112299_~",
});

const db = mongoose.connection;

db.on("error", (err) => {
  console.error("mongoose connect error", err);
});

export default mongoose;
